package cmarter.dev.servlets;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import utils.HttpHelper;
import utils.HttpHelperUtils;
import utils.LoginHelper;
 
	@WebServlet("/sedctv-jenkins/build")
	public class JenkinsController extends HttpServlet {
		private static final long serialVersionUID = -5996833403056476625L;
		
		//TODO: move to properties file
		private String baseUrl = "https://jenkins.di2e.net/job/SED-CTV-paramBuild/buildWithParameters?token=sedctv&BRANCH_NAME=";

		public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
	    	String name =  request.getParameter("branchName");	    
			String url = baseUrl + name;

			try {

				System.out.println(url);
				
				ArrayList<NameValuePair> nvps = new ArrayList <NameValuePair>();
	            nvps.add(new BasicNameValuePair("token", "sedctv"));
	            nvps.add(new BasicNameValuePair("BRANCH_NAME", name));
	            
	            LoginHelper lh = new LoginHelper();
	            lh.Login();
	            
				HttpHelper.doPost(url, name);
/*				HttpHelper.sendPost(name);
				HttpHelper.doWork(name);
				StinkBomb.executeHttpPost(url, nvps);	            
	            StinkBomb.urlExist(url);	            
	            HttpHelper.PostMe(url);

	            StinkBomb.PostIt(url);
	            StinkBomb.executeHttpGet(url);
	            HttpHelper.GetMe(url);*/
				
			} catch (Exception e) {
				e.printStackTrace();
			}
	    	
	        response.getWriter().println(name);
	    }
		
		public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
	    	String name = request.getParameter("branchName");	    	
	    	
	        response.getWriter().println(name);
	    }
	}

